<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	//构造函数
	public function __construct(){
		parent::__construct();
		$this->load->model('user_model','user');
	}
	/*
	 * 注册用户
	 * */
	public function register(){
		$jsonp = $_GET["callback"];
		$user_name = $_GET['user_name'];
		$password = $_GET['password'];
		$use = $this ->user->row(array('user_name' => $user_name));
		if ($use) {
			$data['status'] = 0;
		} else {
			$data = array(
				'user_name' => $user_name,
				'password'	=> $password,
			);
			$re = $this->user->insert($data);
			if ($re) {
				$data['status'] = 1;
			} else {
				$data['status'] = 2;
			}
		}
		echo $jsonp."(".json_encode($data).")";
	}
	
	/*
	 * 进行登录
	 * */
	public function index(){
		$jsonp = $_GET["callback"];
		$user_name = $_GET['user_name'];
		$password = $_GET['password'];
		$data['user_name'] = $user_name;
		$re = $this->user->row(array('user_name' => $user_name));
		if($re){
			$data['status'] = 1;
			if($password == $re['password']){
				$res = $this->admin->login($user_name);
				$data['status'] = 2;
			}
		}else{
			$data['status'] = 0;
		}
		echo $jsonp."(".json_encode($data).")";
	}
	
	/*
	 * 判断登录状态
	 * */
	public function is_login(){
		$this->load->library('session');
		$user = $this->session->userdata('user_name');
		if($user){
			$jsonp = $_GET["callback"];
			$data = array(
				'code'		=> '2000',
				'msg'		=> '用户已登录！！',
				'is_login'	=> 1,
			);
			echo $jsonp."(".json_encode($data).")";
			exit;
		}
	}
	
	public function user_login(){
		$this->load->library('session');
		$user = $this->session->userdata('user_name');
		if(!$user){
			$jsonp = $_GET["callback"];
			$data = array(
				'code'		=> '4000',
				'msg'		=> '用户已退出登录！！',
				'is_login'	=> 1,
			);
			echo $jsonp."(".json_encode($data).")";
			exit;
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */